﻿using Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xiaowei.Settings;

namespace Xiaowei.Services
{
    public class AlarmService
    {
        private static readonly Lazy<AlarmService> LazyInstance = new Lazy<AlarmService>(() =>
        {
            return new AlarmService();
        }, LazyThreadSafetyMode.ExecutionAndPublication);

        public static AlarmService Instance { get { return LazyInstance.Value; } }

        public delegate void AlarmResetUIHandle();
        public event AlarmResetUIHandle ResetUI;
        private AlarmService()
        {
            //ClockManager.Instance.ClockArrived += ClockList_ClockArrvied;
            //ClockManager.Instance.ClockListChanged += Instance_ClockListChanged;

            //add cancel alarm event
            initalAlarmHanle();
        }

        private void Instance_ClockListChanged(object sender, bool e)
        {
            ResetUI?.Invoke();
        }

        private void initalAlarmHanle()
        {
            //string cancelAlarm = "CancelAlarmEvent";
            //ConnectionManager.AddHandler(cancelAlarm, async (v) =>
            //{
            //    string clockId = "clockId";
            //    if (v.ContainsKey(clockId))
            //    {
            //        Debug.WriteLine("AlarmModel CancelAlarmEvent");
            //        long? Clock_id = v[clockId] as long?;
            //        if (Clock_id != null)
            //        {
            //            ClockData clock = ClockManager.Instance.GetClockDataByID(Clock_id.Value);
            //            if (clock != null)
            //            {
            //                _ = HPMetrics.Metrics.Track(XiaoweiAI.Instance.UserId, (int)HPMetrics.XwEventType.ClickButton, "AlarmDelete");
            //                await XiaoweiAI.Instance.ModifyAlarmAsync(3, clock.ToJsonObject()).ConfigureAwait(false);
            //
            //            }
            //        }
            //    }
            //});
        }

        //public event Action<Clock> ClockArrived;


        //private void ClockList_ClockArrvied(object sender, IList<Clock> clock)
        //{
            //string events = "";
            //foreach (var item in clock)
            //{
            //    ClockArrived?.Invoke(item);
            //    if (!string.IsNullOrEmpty(item.ClockData.Clock_event))
            //    {
            //        events += item.ClockData.Clock_event + "\r\n";
            //    }
            //}
            //if (events.Length > 2)
            //{
            //    events = events.Remove(events.Length - 2);
            //}
            //
            //MiniModeAlertModel alertTime = new MiniModeAlertModel();
            //alertTime.TimeString = clock[0].ClockData.DateTime.ToString("HH:mm ");
            //alertTime.ID = clock[0].ClockData.Clock_id;
            //alertTime.Event = events;
            ////send alert time
            //Debug.WriteLine("Alarm arrived!");
            //if (!((SystemState.IsLocked || SystemState.IsSuspended) && !AppState.IsLockscreenUI))
            //{
            //    ClockManager.Instance.RemoveToastFromHistory(clock[0].ClockData.Trig_time);
            //    if (!AppState.IsLockscreenUI)
            //    {
            //        MiniModeService.Instance.AlarmArrieved(alertTime.DataString);
            //    }
            //}

       // }
        public void Begin()
        {
            //ClockManager.Instance.RemoveAll();
            //XiaoweiAI.Instance.FetchAlarmList();
        }
    }
}
